<?php
class Template
{

    public $vars = array();
    public $files = array();
    public $version = "0.1.0";
    public $errortitle = "Template Error";
    public $var_format = "/\\{(\\w+)\\}/e";
    public $lang_format = "/\\{\\{(\\w+)\\}\\}/e";
    public $dir = ".";

    public function Template( $dir = "default" )
    {
        $this->setdir( "tpl/".$dir."/" );
    }

    public function setdir( $dir )
    {
        if ( !is_dir( $dir ) )
        {
            exit( "Cannot found template folder: ".$dir );
        }
        $this->dir = $dir;
        $this->set( "_TPL_PATH", $dir );
        return true;
    }

    public function LoadFromString( $blockname, $blockdata )
    {
        $this->files[$blockname] = $blockdata;
    }

    public function set( $tpl_var, $value = null )
    {
        if ( is_array( $tpl_var ) )
        {
            foreach ( $tpl_var as $key => $val )
            {
                if ( $key != "" )
                {
                    $this->vars[$key] = $val;
                }
            }
        }
        else
        {
            if ( $tpl_var != "" )
            {
                $this->vars[$tpl_var] = $value;
            }
        }
    }

    public function clear( $tpl_var )
    {
        $this->vars = array();
    }

    public function parse( $block )
    {
        if ( !isset( $this->files[$block] ) )
        {
            exit( "not vaild block name :".$block );
        }
        $temp = $this->files[$block];
        $temp = @preg_replace( @$this->lang_format, "\\1", @$temp );
        $temp = @preg_replace( @$this->var_format, "\$this->vars['\\1']", @$temp );
        return $temp;
    }

    public function load( $tpl_var, $value = "" )
    {
        if ( is_array( $tpl_var ) )
        {
            foreach ( $tpl_var as $key => $val )
            {
                if ( $key != "" )
                {
                    if ( !file_exists( $this->dir.$val ) )
                    {
                        exit( "can't find template file :".$this->dir.$val );
                    }
                    $this->files[$key] = implode( "", file( $this->dir.$val ) );
                }
            }
        }
        else
        {
            if ( $tpl_var != "" )
            {
                if ( !file_exists( $this->dir.$value ) )
                {
                    exit( "can't find template file :".$this->dir.$value );
                }
//                echo $this->dir.$value;
                $this->files[$tpl_var] = implode( "", file( $this->dir.$value ) );
            }
        }
    }

    public function show( $block )
    {
        echo $this->parse( $block );
    }

}

class Template2 extends Template
{

    public function Template2( )
    {
        global $op;
        $this->setdir( $op."/tpl/".G_TEMPLATE."/" );
        $this->set( "op", $op );
    }

}

class Template3 extends Template
{

    public function Template3( $title = G_SITENAME_LOGIN )
    {
        $this->setdir( "./loginTpl/tpl_".G_LOGINTPL."/" );
        $this->set( "sitename", $title );
    }

}

class Template_Member extends Template
{

    public function Template_Member()
    {
		global $op;
        $this->setdir( "./skin/".G_TEMPLATE_Member."/" );
        $this->set( "op", $op );
		$this->set("skindir","skin/".G_TEMPLATE_Member."/");
		$this->set( "sitename", G_SITENAME_LOGIN);
    }

}
?>